package com.ickd.configuration;

import cloud.tianai.captcha.cache.CacheStore;
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
import cloud.tianai.captcha.interceptor.CaptchaInterceptorGroup;
import cloud.tianai.captcha.interceptor.impl.ParamCheckCaptchaInterceptor;
import cloud.tianai.captcha.spring.common.util.IPUtils;
import cloud.tianai.mate.captcha.interceptor.group.FeaturesCaptchaInterceptorGroup;
import cloud.tianai.mate.captcha.interceptor.impl.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Configuration
public class CaptchaConfiguration {
    @Autowired
    private SysConfig sysConfig;

    @Bean
    @SneakyThrows
    public CaptchaInterceptor captchaInterceptor(CacheStore cacheStore) {

        CaptchaInterceptorGroup group = new CaptchaInterceptorGroup();
        // 参数基本校验
        group.addInterceptor(new ParamCheckCaptchaInterceptor());
        // 预生成轨迹特征
        group.addInterceptor(new TrackFeaturesGenerator());


        // 云端轨迹等信息拦截器组
//        FeaturesCaptchaInterceptorGroup group2 = new FeaturesCaptchaInterceptorGroup(cacheStore, (d) -> {
//            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//            // 这里用于读取用户唯一标识，这里暂时用ip作为唯一标识
//            return "captcha:features:" + IPUtils.getCurrentIp(requestAttributes);
//        });

//        // 启始位置拦截器， 不是很稳定，如果报错率过高，考虑把它去爱到
//        group2.addInterceptor(new SliderStartPosCaptchaInterceptor());
//        // 轨迹相似性校验拦截器，防止灰产用同一条或者，按照某种规律随机的轨迹校验
//        group2.addInterceptor(new SliderTrackSimilarityCaptchaInterceptor());
//        // 请求次数校验拦截器，默认拦截用户一定时间内只能校验X次，可自己修改
////        CaptchaRequestNumInterceptor numInterceptor = new CaptchaRequestNumInterceptor();
////        numInterceptor.setCheckNum(300);
////        group2.addInterceptor(numInterceptor);
//        // 差异校验拦截器，校验每次到缺口的差异
//        group2.addInterceptor(new DifferenceCaptchaInterceptor());
//        // 禁用拦截，满足条件后会直接禁用该用户访问
//        group2.addInterceptor(new DisableCaptchaInterceptor());
//
//        group.addInterceptor(group2);

        // xgboost滑动轨迹校验
//        InputStream inputStream = new ClassPathResource("model/xgb-model-4-12.json").getInputStream();
        InputStream inputStream =new FileInputStream(new File(sysConfig.getResourcePath()+"/model/xgb-model-4-12.json"));
        XGBTrackCaptchaInterceptor xgboost = new XGBTrackCaptchaInterceptor(inputStream);
        group.addInterceptor(xgboost);

        // 干扰拦截器组
        CaptchaInterceptorGroup group3 = new CaptchaInterceptorGroup("processor_group");
        // 对文字点选类图片加压字干扰,会在正确文字上班随机覆盖一个白色的字，
//        group3.addInterceptor(new WordImageClickInterfereProcessor());
        // 将背景图片乱序，可以在前端进行重组，注意: 小程序对图片重组可能会有点卡
        group3.addInterceptor(new ShuffleImageProcessor());

        group.addInterceptor(group3);

        System.out.println("==============================验证码拦截器==============================\n");
        // 控制台打印一下拦截器列表
        System.out.println(group.printTree());
        System.out.println("==============================验证码拦截器==============================\n");
        return group;
    }
}
